//==================
//Array
//==================
// Array remove <removeobj> func 
Array.prototype.remove = function (robj) {
    var idx = this.indexOf(robj)
    if (idx > -1) {
        this.splice(idx, 1)
        return true
    }
    return false
}
Object.defineProperty(Array.prototype, 'remove', {
    enumerable: false
});

// Array add <sortDesc> func
Array.prototype.sortDesc = function (a, b) {
    var args = arguments;
    var _sortFunc = function (a, b, idx) {
        if (args[idx + 1]) {
            if (a[args[idx]] == b[args[idx]]) return _sortFunc(a, b, idx + 1);
        }
        if (!b[args[idx]]) return -1;
        if (!a[args[idx]]) return 1;
        return b[args[idx]] - a[args[idx]];
    };
    this.sort(function (a, b) { return _sortFunc(a, b, 0) });
};
Object.defineProperty(Array.prototype, 'sortDesc', {
    enumerable: false
});
Array.prototype.sum = function () {
    for (var sum = i = 0; i < this.length; i++)sum += parseInt(this[i]);
    return sum

};
Object.defineProperty(Array.prototype, 'sum', {
    enumerable: false
});


// Array add <sortAsc> func
Array.prototype.sortAsc = function (a, b) {
    var args = arguments;
    var _sortFunc = function (a, b, idx) {
        if (args[idx + 1]) {
            if (a[args[idx]] == b[args[idx]]) return _sortFunc(a, b, idx + 1);
        }
        if (!a[args[idx]]) return -1;
        if (!b[args[idx]]) return 1;
        return a[args[idx]] - b[args[idx]];
    };
    this.sort(function (a, b) { return _sortFunc(a, b, 0) });
};
Object.defineProperty(Array.prototype, 'sortAsc', {
    enumerable: false
});


// Array add <insert> func
Array.prototype.insert = function (index, item) {
    this.splice(index, 0, item);
};
Object.defineProperty(Array.prototype, 'insert', {
    enumerable: false
});

//Array global func
var table = {};
table.copy = function (obj) {
    var newObj = (obj.constructor) ? new obj.constructor : {};
    for (var key in obj)
        newObj[key] = obj[key];
    return newObj;
}
table.deepCopy = function (obj) {
    var newObj = (obj.constructor) ? new obj.constructor : {};
    for (var key in obj) {
        var copy = obj[key];
        if (((typeof copy) === "object") && copy) {
            newObj[key] = table.deepCopy(copy);
        }
        else {
            newObj[key] = copy;
        }
    }
    return newObj;
}
table.nums = function (obj) {
    var nums = 0;
    for (var k in obj) {
        nums++;
    }
    return nums;
}
table.keys = function (obj) {
    var keys = [];
    for (var k in obj) {
        keys.push(k);
    }
    return keys;
}
table.values = function (obj) {
    var values = [];
    for (var k in obj) {
        values.push(obj[k]);
    }
    return values;
}

//==================
//String global func
//==================
String.prototype.replaceAll = function (_old, _new) {
    return this.replace(new RegExp(_old, 'g'), _new)
};
String.prototype.capitalize = function () {
    return this.substring(0, 1).toUpperCase() + this.substring(1);
};

//==================
//Global func
//==================
//callback bind args 
function c_func(func) {
    var _args = Array.prototype.slice.call(arguments, 1)
    return function () {
        return func.apply(this, _args.concat(Array.prototype.slice.call(arguments)));
    }
}

//new Class by clsobject
function cls_new(Cls, args) {
    function _C() {
        return Cls.apply(this, args);
    }
    _C.prototype = Cls.prototype;
    return new _C();
}

//hook 
function func_hook(obj, proname) {
    if (obj[proname]) {
        obj[proname](Array.prototype.slice.call(arguments, 2))
    }
}
//转换成整型 
function toint(o) {
    o = parseInt(o);
    if (!o) o = 0;
    return o;
}

export { func_hook, toint, cls_new, c_func };